package com.enterprise.controller.api.banner;

import com.alibaba.fastjson.JSON;
import com.enterprise.controller.api.banner.vo.BannerResp;
import com.enterprise.controller.api.common.BaseResponse;
import com.enterprise.entity.IndexImg;
import com.enterprise.service.IndexImgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RequestMapping("/api/banner")
@Controller("apiBannerController")
public class BannerController {

    @Autowired
    private IndexImgService bannerService;

    @ResponseBody
    @RequestMapping("/queryBannerList")
    public BaseResponse<List<BannerResp>> queryCarList(){
        BaseResponse response = new BaseResponse();
        List<IndexImg> bannerList = bannerService.selectList(new IndexImg());
        List<BannerResp> bannerResps = new ArrayList<>();
        for (IndexImg banner:bannerList) {
            BannerResp bannerResp = new BannerResp();
            BeanUtils.copyProperties(banner,bannerResp);
            bannerResp.setPictureUrl(BaseResponse.httpstr(bannerResp.getPictureUrl()));
            bannerResps.add(bannerResp);
        }
        log.info("queryBannerList bannerResps: {}" , JSON.toJSONString(bannerResps));
        response.setData(bannerResps);
        return response;
    }


    @ResponseBody
    @RequestMapping("/queryBannerView/{id}")
    public BaseResponse<BannerResp> queryBannerView(@PathVariable("id") String id){
        BaseResponse response = new BaseResponse();
        IndexImg banner = bannerService.selectById(Integer.valueOf(id));
        BannerResp bannerResp = new BannerResp();
        BeanUtils.copyProperties(banner,bannerResp);
        bannerResp.setPictureUrl(BaseResponse.httpstr(bannerResp.getPictureUrl()));
        log.info("queryBannerView bannerResp: {}" , JSON.toJSONString(bannerResp));
        response.setData(bannerResp);
        return response;
    }


}
